﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace workshop3
{
    public partial class Formeditrubric : Form
    {
        DataTable rubric = new DataTable();
        DataColumn itemname = new DataColumn("itemname");
        DataColumn percentage = new DataColumn("percentage");
        List<string> yearlist = new List<string>();
        List<string> semelist = new List<string>();
        string courseid;
        string name;
        bool save = false;
        int totalpercentage = 0;
        public Formeditrubric(DataTable rubric, List<string> year, List<string> seme,string courseid,string name)
        {
            InitializeComponent();
            //this.itemname=rubric.Columns[1];
            //this.percentage = rubric.Columns[2];
            //this.rubric.Columns.Add(this.itemname);
            //this.rubric.Columns.Add(this.percentage);
            this.rubric = rubric;
            //this.rubric.Columns.Remove(this.rubric.Columns[0]);
            this.yearlist = year;
            this.semelist = seme;
            this.courseid = courseid;
            this.name = name;
            txtrubricname.Text = this.name;
            
        }

        private void Formeditrubric_Load(object sender, EventArgs e)
        {
            dgvrubirc.DataSource = rubric;
            //dgvrubirc.Columns[0].Visible = false;
            //before the datasource has been binded, the event should be disable, because during the data loading into combobox, the value wil be changed and activate the value change event
            cbyear.SelectedValueChanged -= cbyear_SelectedValueChanged;
            cbsemester.SelectedValueChanged -= cbsemester_SelectedValueChanged;
            cbcourse.SelectedValueChanged -= cbcourse_SelectedValueChanged;
            cbyear.DataSource = yearlist;
            cbsemester.DataSource = semelist;
            //after the data binding finished, enable the event
            cbyear.SelectedValueChanged += cbyear_SelectedValueChanged;
            cbsemester.SelectedValueChanged += cbsemester_SelectedValueChanged;
        }

        private void ChangeCourse(string year, string semester)//the same function used in the courseview part
        {
            DataTable dt0 = new DataTable();
            DataColumn dc3 = new DataColumn("coursename");
            DataColumn dc4 = new DataColumn("courseid");
            dt0.Columns.Add(dc4);
            dt0.Columns.Add(dc3);
            if (File.Exists("course.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("course.xml");
                string path = "//course";
                XmlNodeList nodelist = doc.SelectNodes(path);
                if (nodelist.Count != 0)
                {
                    for (int i = 0; i < nodelist.Count; i++)
                    {
                        if (nodelist[i].ChildNodes[1].InnerText == year && nodelist[i].ChildNodes[2].InnerText == semester)
                        {
                            dt0.Rows.Add(nodelist[i].Attributes[0].Value, nodelist[i].ChildNodes[0].InnerText);
                        }

                    }

                    cbcourse.DataSource = dt0;
                    cbcourse.DisplayMember = dt0.Columns[1].ToString();
                    cbcourse.ValueMember = dt0.Columns[0].ToString();
                    cbcourse.SelectedValueChanged += cbcourse_SelectedValueChanged;


                }
                else
                {
                    MessageBox.Show("Can not find any course!");
                }

                //return dt0;

            }
            //return null;
        }

        private void cbcourse_SelectedValueChanged(object sender, EventArgs e)
        {
            courseid = cbcourse.SelectedValue.ToString();
            //ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
        }

        private void cbyear_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
        }

        private void cbsemester_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (totalpercentage == 100)//check if the total percentage is equal to 100 or not
            {
                save = true;
                if (ClassCreateXML.AddRubricXml(rubric, txtrubricname.Text, courseid))
                {
                    MessageBox.Show("Save successfully");
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("The total percentage is illegal");
            }

        }

        private void Formeditrubric_FormClosing(object sender, FormClosingEventArgs e)//this function is to control the closing of the window
        {
            if (save==false)//if the user do not click the save button, the window will not be allowed to closed
            {
                e.Cancel = true;
                MessageBox.Show("You have not save this rubric!");
                //e.Cancel = false;
                //this.Close();
                
            }
            else
            {
                e.Cancel = false;
            }
        }

        private void dgvrubirc_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)//detect the mouse click is the right buttom
            {
                totalpercentage = 0;
                int temp;
                //int.TryParse(dtams.Rows[e.RowIndex][1].ToString(), out temp);
                //percentage = percentage - temp;
                rubric.Rows.Remove(rubric.Rows[e.RowIndex]);
                for (int i = 0; i < rubric.Rows.Count; i++)
                {
                    int.TryParse(rubric.Rows[i][1].ToString(), out temp);
                    totalpercentage = totalpercentage + temp;
                }
                lbltotal.Text = totalpercentage.ToString();
            }
        }

        private void dgvrubirc_CellValueChanged(object sender, DataGridViewCellEventArgs e)//if the data change in the datagridview, recalculate the total percentage
        {
            totalpercentage = 0;
            int temp;
            //int.TryParse(dtams.Rows[e.RowIndex][1].ToString(), out temp);
            for (int i = 0; i < rubric.Rows.Count; i++)
            {
                int.TryParse(rubric.Rows[i][1].ToString(), out temp);
                totalpercentage = totalpercentage + temp;
            }
            lbltotal.Text = totalpercentage.ToString();
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            //first check the input is legal or not
            if (!ClassCreateXML.TextCheck(txtitemname.Text))
            {
                return;
            }
            if (ClassCreateXML.TextCheck(txtpercentage.Text))
            {
                return;
            }
            if (txtitemname.Text != null && txtpercentage.Text != null)
            {
                int temp = 0;
                int.TryParse(txtpercentage.Text, out temp);
                totalpercentage = totalpercentage + temp;
                if (totalpercentage <= 100)
                {
                    rubric.Rows.Add(txtitemname.Text, txtpercentage.Text);
                    dgvrubirc.DataSource = rubric;
                }
                else
                {
                    MessageBox.Show("The total percentage is overhead 100. Please input valid percentage!");
                    totalpercentage = totalpercentage - temp;
                }
                txtitemname.Text = string.Empty;
                txtpercentage.Text = string.Empty;
                lbltotal.Text = totalpercentage.ToString();
            }
        }
    }
}
